package org.xawl.news.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.xawl.news.entity.role.Role;
import org.xawl.news.entity.user.User;

public class SessionUtil {
	
	// session中获取当前用户
	private static final String CURRENT_USER = "CURRENT_USER";
	// session中获取图片验证码
	private static final String CURRENT_IMAGE_CODE = "CURRENT_IMAGE_CODE";
	// 当前角色
	private static final String CURRENT_ROLE = "CURRENT_ROLE";
	
	// 获取当前用户
	public static User getCurrentUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute(CURRENT_USER);
		return user;
	}
	
	// 设置当前用户
	public static void setCurrentUser(HttpServletRequest request, User user) {
		HttpSession session = request.getSession();
		session.setAttribute(CURRENT_USER, user);
	}
	
	// 获取当前用户
	public static Role getCurrentRole(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Role role = (Role)session.getAttribute(CURRENT_ROLE);
		return role;
	}
	
	// 设置当前用户
	public static void setCurrentRole(HttpServletRequest request, Role role) {
		HttpSession session = request.getSession();
		session.setAttribute(CURRENT_ROLE, role);
	}
	
	// 获取图片验证码
	public static String getCurrentImageCode(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String code = (String)session.getAttribute(CURRENT_IMAGE_CODE);
		return code;
	}
	
	// 设置当前图片验证码
	public static void setCurrentImageCode(HttpServletRequest request, String code) {
		HttpSession session = request.getSession();
		session.setAttribute(CURRENT_IMAGE_CODE, code);
	}
		
}
